How not to use the ON command.
NOTE: This is a copy of a postCommon error in using On and solution on KSP Forum. originally made in the Kerbal Space Program Forum. It's been copied here for posterity in case the URL becomes invalid as often happens with forums when things get re-arranged. Orginal post follows In numerous examples and code the ON command has been used wrongly. Very Wrongly. I suspected it didn't make sense how people were using it when I first saw it but since everyone was already doing things this way I assumed it must just not work the way I intuitively guessed it did. A recent issue raised in the github made me go back and revisit that assumption. I started suspecting again that my original suspicions were right, given a complaint about how loops get very slow and memory usage leaks when a loop has an ON statement in it. I made a test to see if it works the way I originally thought it did, and it does, and the way we've been using it is VERY wrong. This small example program illustrates the problem: done off. until done { on ag1 print "action group 1!". on ag2 done on. wait 1. print "loop iteration.". }. Looking at the code and thinking the way people writing their KOS code have been thinking, you'd think it would just loop forever one second per iteration, and then when you press ALT-1 it will print "action group 1!" and then when you press ALT-2 it ends, like so: loop iteration. loop iteration. loop iteration. loop iteration. loop iteration. action group 1! <-- user pressed ALT-1 loop iteration. loop iteration. loop iteration. action group 1! <-- user pressed ALT-1 loop iteration. Program ended. <-- user pressed ALT-2 This is in fact NOT what it does. Each time you execute the ON command it inserts a brand NEW event trigger into the watchlist even though one was already there. Therefore what the code actually does is this: loop iteration. loop iteration. loop iteration. loop iteration. loop iteration. action group 1! <-- user pressed ALT-1 action group 1! action group 1! action group 1! action group 1! loop iteration. loop iteration. loop iteration. action group 1! <-- user pressed ALT-1 action group 1! action group 1! loop iteration. Program ended. <-- user pressed ALT-2 It prints out the statement once per iteration of the loop that had previously occurred since the last time AG1 was pressed. This is because each time ON... is being executed it's inserting a NEW hook into the condition watcher. My example makes sure the numbers are small so you can see what's happening, but now try it again with the "wait 1" removed. Now it spews out a LOT of "action group 1!" lines because the loop was running at full speed and running the "ON" command a lot of times. It also was inserting lots of the action group 2 triggers as well, but since all that trigger does is set the boolean variable "done" to true, you don't notice. If it sets "done" to true once or does so 100 times the effect is that same in the end. So this has been happening all along and we didn't notice. This goes back to a comment I made a long time ago in this thread about how we don't REALLY have the ability to set up a trigger that re-executes EACH time a condition is true. This practice of putting an ON inside a loop is NOT enough to fix that problem, as it creates this new problem of clogging the mod with zillions of redundant pointless trigger hooks. I think we really could use a way to differentiate "EACH TIME X is true do Y" from "JUST THE NEXT TIME X is true do Y just once". in the language. In the meantime, I *THINK* this might fix the problem (I don't have KSP running right now so I can't test it. But you can see what the logic is meant to be): Change instances of this: until blah { on AG1 someCommandHere. restOfLoopBodyHere. }. To this instead: insertAG1Hook on. until blah { if insertAG1Hook { insertAG1Hook off. on AG1 insertAG1Hook on. on AG1 someCommandHere. }. restOfLoopBodyHere. } Basically it only inserts a new AG1 if an AG1 hook has just happened. If the previous one has not triggered it won't make another one. __notoc__ Category:Tutorials Category:Examples